package com.data;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.commun.ArabicUtilities;
import com.object.Mosque;

public class DataUtils {

	public static List<Mosque> ExtracteMosquesFromHttpRequest(String request) {
		List<Mosque> mosquais = new ArrayList<Mosque>();

		if (request.toString().length() > 0) {

			Pattern p = Pattern.compile("latlng:\\{"); // the expression
			String[] tmp = p.split(request);
			// Utils.AlertTestDeveloppementMsg(tmp.length+"");

			for (String mosqueTmp : tmp) {
				if (mosqueTmp.startsWith("while"))
					continue;
				p = Pattern.compile("\\}"); // the expression
				Matcher m = p.matcher(mosqueTmp); // the source
				m.find();
				String latlng = mosqueTmp.substring(0, m.start());
				p = Pattern.compile(",");
				m = p.matcher(latlng);

				m.find();
				Double lat = Double.parseDouble(latlng.substring(4, m.start()));
				Double lng = Double
						.parseDouble(latlng.substring(m.start() + 5));

				p = Pattern.compile("laddr:\""); // the expression
				m = p.matcher(mosqueTmp);
				m.find();
				int addresseStart = m.start() + 7;

				p = Pattern.compile("\\("); // the expression
				m = p.matcher(mosqueTmp.substring(addresseStart));
				m.find();

				int addresseEnd = m.start() + addresseStart;
				String addresse = mosqueTmp.substring(addresseStart,
						addresseEnd);

				p = Pattern.compile("\\)"); // the expression
				m = p.matcher(mosqueTmp.substring(addresseEnd));
				m.find();
				String nom = mosqueTmp.substring(addresseEnd + 1, m.start()
						+ addresseEnd);
				if (nom.contains("&#")) {

					nom = ArabicUtilities.getInstance()
							.ReshapeInArabicWord(nom);

				}

				Mosque mosque = new Mosque(nom, lat, lng, addresse);
				mosquais.add(mosque);
			}
		}
		return mosquais;
	}
}
